using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class OrdersAccessor:IOrdersRepository
    {
        public override int Insert(Orders entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@OrderID","Orders_Insert",entity.OrderID,entity.PersonID,entity.Tax,entity.Totals,entity.Notes);
            return int.Parse(rs.ToString());
        }
        public override int Update(Orders entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_Update",entity.OrderID,entity.PersonID,entity.DateCreated,entity.Tax,entity.Totals,entity.Notes);
            return rs ;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Orders_Delete", id) > 0;
        }
        public override bool Delete(Orders entity)
        {
            return Delete(entity.OrderID);
        }
        public override List<Orders> FindAll()
        {
            return CBO.FillCollection<Orders>(IDataProvider.Instance.ExecuteReader("Orders_FindAll"));
        }
        public override Orders FindById(int id)
        {
            return CBO.FillObject<Orders>(IDataProvider.Instance.ExecuteReader("Orders_FindById", id));
        }
        public override Orders Single(int id)
        {
            return FindById(id);
        }
        public override Orders FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<Orders> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Orders_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Orders>(reader);
        }
        public override List<Orders> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
             var query = from c in FindAll()
                    select c;
                    totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Orders_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Orders_RestoreAll");
            return rs > 0;
        }
        public override List<OrderDetail> FindOrderDetail(int orderId)
        {
            return CBO.FillCollection<OrderDetail>(IDataProvider.Instance.ExecuteReader("Orders_FindOrderDetail", orderId));
        }
    }
}